package com.ssy.lingxi.platform.manage.serviceimpl;

import cn.hutool.core.collection.CollectionUtil;
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import com.ssy.lingxi.common.constant.basic.EnableDisableStatus;
import com.ssy.lingxi.common.exception.BusinessException;
import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.common.response.ResponseCode;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.platform.manage.constant.CommonBooleanEnum;
import com.ssy.lingxi.platform.manage.entity.seo.QSeo;
import com.ssy.lingxi.platform.manage.entity.seo.Seo;
import com.ssy.lingxi.platform.manage.model.request.common.CommonIdRequest;
import com.ssy.lingxi.platform.manage.model.request.common.CommonStatusRequest;
import com.ssy.lingxi.platform.manage.model.request.seo.SeoListRequest;
import com.ssy.lingxi.platform.manage.model.request.seo.SeoRequest;
import com.ssy.lingxi.platform.manage.repository.SeoRepository;
import com.ssy.lingxi.platform.manage.service.ISeoService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 平台后台 - SEO优化 - SEO设置 - 业务实现层
 *
 * @author fhj
 * @version 2.0.0
 * @date 2021/04/07
 */
@Service
public class SeoServiceImpl implements ISeoService {

    @Resource
    private SeoRepository seoRepository;
    @Resource
    private JPAQueryFactory jpaQueryFactory;

    @Override
    public Wrapper<PageData<Seo>> page(SeoListRequest request, UserLoginCacheDTO user) {
        QSeo qSeo = QSeo.seo;
        JPAQuery<Seo> jpaQuery = jpaQueryFactory.select(qSeo).from(qSeo);
        if (StringUtils.isNotBlank(request.getName())) {
            jpaQuery.where(qSeo.name.contains(request.getName()));
        }
        long totalCount = jpaQuery.fetchCount();
        List<Seo> seoList = jpaQuery.orderBy(qSeo.createTime.desc()).limit(request.getPageSize()).offset((long) (request.getCurrent() - 1) * request.getPageSize()).fetch();
        return Wrapper.success(new PageData<>(totalCount, seoList));
    }

    @Override
    public Wrapper<Seo> get(CommonIdRequest request, UserLoginCacheDTO user) {
        Seo seo = seoRepository.findById(request.getId()).orElse(null);
        if (seo == null) {
            throw new BusinessException(ResponseCode.MAN_RECORDS_DON_T_EXIST);
        }
        return Wrapper.success(seo);
    }

    @Override
    @Transactional
    public Wrapper<Void> add(SeoRequest request, UserLoginCacheDTO user) {
        Seo seo = new Seo();
        BeanUtils.copyProperties(request, seo);
        seoRepository.saveAndFlush(seo);
        return Wrapper.success();
    }

    @Override
    @Transactional
    public Wrapper<Void> update(SeoRequest request, UserLoginCacheDTO user) {
        Seo seo = seoRepository.findById(request.getId()).orElse(null);
        if (seo == null) {
            throw new BusinessException(ResponseCode.MAN_RECORDS_DON_T_EXIST);
        }
        if (seo.getStatus().equals(EnableDisableStatus.ENABLE.getCode())) {
            throw new BusinessException(ResponseCode.MAN_DATA_STATUS_INCORRECT_OPERATE_INVALID);
        }
        BeanUtils.copyProperties(request, seo);
        seoRepository.saveAndFlush(seo);
        return Wrapper.success();
    }

    @Override
    @Transactional
    public Wrapper<Void> updateStatus(CommonStatusRequest request, UserLoginCacheDTO user) {
        Seo seo = seoRepository.findById(request.getId()).orElse(null);
        if (seo == null) {
            throw new BusinessException(ResponseCode.MAN_RECORDS_DON_T_EXIST);
        }
        seo.setStatus(request.getEnableStatus());
        seoRepository.saveAndFlush(seo);
        return Wrapper.success();
    }

    @Override
    @Transactional
    public Wrapper<Void> delete(CommonIdRequest request, UserLoginCacheDTO user) {
        Seo seo = seoRepository.findById(request.getId()).orElse(null);
        if (seo == null) {
            throw new BusinessException(ResponseCode.MAN_RECORDS_DON_T_EXIST);
        }
        if (seo.getStatus().equals(EnableDisableStatus.ENABLE.getCode())) {
            throw new BusinessException(ResponseCode.MAN_DATA_STATUS_INCORRECT_OPERATE_INVALID);
        }
        seoRepository.delete(seo);
        return Wrapper.success();
    }

    @Override
    public Wrapper<Seo> byType(Integer type) {
        List<Seo> seoList = seoRepository.findByTypeAndStatus(type, CommonBooleanEnum.YES.getCode());
        if (CollectionUtil.isNotEmpty(seoList)) {
            return Wrapper.success(seoList.stream().limit(1).findFirst().orElse(new Seo()));
        }

        return Wrapper.success(new Seo());
    }
}
